package cn.jinzhu.netty.server.handler;

import cn.jinzhu.netty.message.GroupChatRequestMessage;
import cn.jinzhu.netty.message.GroupChatResponseMessage;
import cn.jinzhu.netty.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

/**
 * 群聊 这个变量是可以共享的 所以在netty中被多线程共享的需要加
 */
@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage msg) throws Exception {
        List<Channel> channels = GroupSessionFactory.getGroupSession()
                .getMembersChannel(msg.getGroupName());

        //一个channel通道一个用户  循环通道获取所以用户  给其他用户提示 这个用户被拉入了群 群发 获取每个用户的管道channel输入群发数据
        for (Channel channel : channels) {
            channel.writeAndFlush(new GroupChatResponseMessage(msg.getFrom(), msg.getContent()));
        }
    }
}
